import 'package:dio/dio.dart';
import 'dart:async';
import 'dart:io';
import '../config/server.dart';

 const httpHeaders = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Encoding': 'gzip, deflate, br',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    'Cookie': '_ga=GA1.2.553531781.1555553293; _gid=GA1.2.1926651818.1561105456; Hm_lvt_022f847c4e3acd44d4a2481d9187f1e6=1561105456; Hm_lpvt_022f847c4e3acd44d4a2481d9187f1e6=1561105507; SERVERID=3431a294a18c59fc8f5805662e2bd51e|1561105515|1561105453; _gat=1',
    'Host': 'time.geekbang.org',
    'Origin': 'https://time.geekbang.org',
    'Pragma': 'no-cache',
    'Referer': 'https://time.geekbang.org/',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
};

// 获取首页主题内容
Future homePageContent () async {
  try {
   print('--------------------- get home Content -----------------');
    Response response;
    Dio dio = new Dio();
    dio.options.contentType = ContentType.parse('application/x-www-form-urlencoded');
    var formData = {"lon": "115.02932", "lat": "35.76189"};

    response = await dio.post(
      pagePath['homePageContent'],
      data: formData);
      print(response);
      if (response.statusCode == 200) {
        return response;
      } else {
        throw Exception('请求首页数据异常');
      }
  } catch (e) {
    print('--------------- get home error -----------------------');
    return e;
  }
}

Future bannerContent () async {
  print('--------------------- get banner Content -----------------');
  try {
    Response response;
    Dio dio = new Dio();
    dio.options.headers = httpHeaders;
    response = await dio.get(pagePath['bannerContent']);
    if (response.statusCode == 200) {
      return response;
    } else {
      throw Exception('请求banner 错误');
    }
  } catch (e) {
    print('--------------- get banner error -----------------------');
    return e;
  }
}